<?php
namespace Home\Controller;
use Think\Controller;
class CourseController extends Controller {
    
     public function courselist(){//课程列表-赵明月
        $admin = M('clubadmin');
        $c_tel=$_SESSION['c_tel'];
        $admins=$admin->where("c_tel='$c_tel'")->find();
        $this->admin=$admins;
       $clubname=$admin->clubname;
       $clubs = M('club');
       $res = $clubs->where("club_name='$clubname'")->select();
       $id=$res['0']['cid'];
       $courses = M('course');
       $res1 = $courses->where("cid='$id'")->select();
       $this->qrcodeList=$res1;

    $qrcodenum=$courses->where("cid='$id'")->count();
    $Page = new \Think\Page($qrcodenum,3);
    $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
   每页<b>3</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
    $Page->setConfig('prev','上一页');
    $Page->setConfig('next','下一页');
    $Page->setConfig('last','末页');
    $Page->setConfig('first','首页');
    $Page->setConfig('theme',' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
    $this->pageNav=$Page->show();//在页面{pageNav}
        //$this->link=$Page->show();
    $this->qrcodeList=$courses->where("cid='$id'")->limit($Page->firstRow,3)->order('id asc')->select();
       $this->display();


    }

public function delcourse($id){//删除课程-赵明月
  
        $coursesModel=M('course');
        if ($coursesModel->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }

    public function addcourse(){//添加课程-赵明月
       
        $admin = M('clubadmin');
        $c_tel=$_SESSION['c_tel'];
        $admins=$admin->where("c_tel='$c_tel'")->find();
        $this->admin=$admins;
        $clubname=$admin->clubname;

        $clubs = M('club');
        $res = $clubs->where("club_name='$clubname'")->select();
        $cids=$res['0']['cid'];

        $teachers = M('teacher');
        $res1 = $teachers->where("cid='$cids'")->select(); 
        $this->list1=$res1;
        $this->display();

    }

    Public function docourse(){//添加课程-赵明月
        $data=$_POST;
        $file=$_FILES;
        $courseModel = M('course'); // 实例化User模型
        $courseModel->course_name =  I('post.course_name'); // 设置用户的创建时间
        $courseModel->uploaddate =  date("Y-m-d H:i:s"); // 设置用户的创建时间
        $courseModel->course_time = I('post.course_time');
        $courseModel->course_price =  I('post.course_price');
        $courseModel->course_level =  I('post.course_level');
        $courseModel->course_intro =  I('post.course_intro'); 
        if ($file['course_pic']['size']==0) {//没有上传轮播图片
            //$this->error('请上传背景图片！');                         
        }else{//有图片
            $info=$this->MyUpload();
            $courseModel->course_pic = $info['course_pic']['savename']; // 保存上传的照片根据需要自行组装
        }
        $admin = M('clubadmin');
        $c_tel=$_SESSION['c_tel'];
        $admins=$admin->where("c_tel='$c_tel'")->find();
        $this->admin=$admins;
        //var_dump($admins);
        $clubname=$admin->clubname;
        //var_dump($clubname);
        $clubs = M('club');
        $res = $clubs->where("club_name='$clubname'")->select();
        //var_dump($res);
        $cid=$res['0']['cid'];
        $courseModel->cid=$cid;
        if(false!=$courseModel->add()){
                $this->success('添加成功!',U('courselist'));
            }else{$this->error('数据错误');}

        
    }

     public function MyUpload(){//上传图片-赵明月
                //上传头像
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 100000000000 ;// 设置附件上传大小
        //$upload->allowExts  = array('mp4','jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg','mp4');
        $upload->rootPath  =  './';
        $upload->savePath =  'Public/upload/coursepic/';// 设置附件上传目录

        $upload->autoSub = false;
        $upload->saveName = date("YmdHis").rand(100,999);//定义名字
        // 上传单个文件 
        $info = $upload->upload();                    
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                return $info;
            }
    } 
 function editcourse($id){//修改课程-赵明月
        $courses = M('course');
        $res = $courses->where("id = $id")->find($data);
        $this->list=$res;
        $this->display();
    }
    function doUpdate($id){//修改课程-赵明月
   
        $coursesmodel=M('course');
        $data['course_name']=I('post.course_name');
        $data['course_level']=I('course_level');
        $data['course_price']=I('post.course_price');
        $data['course_time'] =  I('post.course_time');
        $data['course_intro'] =  I('post.course_intro');
            if($_FILES['course_pic']['error'] == 0){
            $logoinfo = $this->MyUpload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
          $data['course_pic'] = $pname;
        $res=$coursesmodel->where("id = $id")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','courselist');
        } else{
            $this->error('信息修改失败');
    }

}


}